/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.figures;

import org.eclipse.draw2d.Connection;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Locator;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;

/**
 * this class is used to caludlate the possition of the middle of a link.
 * 
 * @author PT202707
 */
public class MiddleEndpointLocator implements Locator {

	/**
	 * 
	 */
	private Point result=null;
	
	/**
	 * 
	 * 
	 * @param c 
	 * @param p 
	 */
	public MiddleEndpointLocator(Connection c, Point p) {
		conn = c;
		figureBounds = new Rectangle();
		setDistances (p);
	}

	/**
	 * 
	 * 
	 * @param p 
	 */
	public void setDistances (Point p) {
		setXDistance(p.x);
		setYDistance(p.y);
	}
	

	
	/**
	 * 
	 * 
	 * @return 
	 */
	private Connection getConnection() {
		return conn;
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	public Point getLocation(){
		Connection conn = getConnection();
		Point startPoint = Point.SINGLETON;
		Point endPoint = new Point();

		int startPointPosition = 0;
		int endPointPosition = 1;
		

		conn.getPoints().getPoint(startPoint, startPointPosition); //get connnections ends
		conn.getPoints().getPoint(endPoint, endPointPosition);

		
		
		int xxx = (startPoint.x+endPoint.x)/2 + xDistance; //calculate middle
		int yyy = (startPoint.y+endPoint.y)/2 + yDistance;
		result= new Point(xxx,yyy);
		
		return result;}
	

	/**
	 * 
	 * 
	 * @param figure 
	 */
	public void relocate(IFigure figure) {
		Connection conn = getConnection();
		Point startPoint = Point.SINGLETON;
		Point endPoint = new Point();

		int startPointPosition = 0;
		int endPointPosition = 1;
		

		conn.getPoints().getPoint(startPoint, startPointPosition);
		conn.getPoints().getPoint(endPoint, endPointPosition);

		
		Dimension figureSize = figure.getPreferredSize();
		int xxx = (startPoint.x+endPoint.x)/2 + xDistance;
		int yyy = (startPoint.y+endPoint.y)/2 + yDistance;
		
		
		figureBounds.setSize(figureSize);
		figureBounds.setLocation(new Point (xxx, yyy));
		figure.setBounds(figureBounds);
	}

	/**
	 * 
	 * 
	 * @param distance 
	 */
	public void setXDistance(int distance) {
		xDistance = distance;
	}

	/**
	 * 
	 * 
	 * @param distance 
	 */
	public void setYDistance(int distance) {
		yDistance = distance;
	}


	/**
	 * 
	 */
	private Connection conn;
	
	/**
	 * 
	 */
	private int xDistance, yDistance;
	
	/**
	 * 
	 */
	private static Rectangle figureBounds;

}